package com.mt.yanyou.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mt.yanyou.MainActivity;
import com.mt.yanyou.R;
import com.mt.yanyou.entity.User;
import com.mt.yanyou.register.RegisterActivity;
import com.mt.yanyou.utils.DialogUtil;
import com.mt.yanyou.utils.SharedPreferencesUtil;
import com.victor.loading.book.BookLoading;
import com.victor.loading.newton.NewtonCradleLoading;
import com.victor.loading.rotate.RotateLoading;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.refactor.lib.colordialog.PromptDialog;


@ContentView(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity {
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    private RotateLoading bookLoading;
    @ViewInject(R.id.btn_login)
    Button btn_login;
@ViewInject(R.id.et_login)
EditText et_login;
    @ViewInject(R.id.et_pass)
    EditText et_pass;
@ViewInject(R.id.cb_rm)
    CheckBox cb_rm;

//登录
@Event(value = R.id.btn_login)
private  void login(View v){
    btn_login.setText("登陆中，请稍后");
    bookLoading.start();
    bookLoading.setVisibility(View.VISIBLE);
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    btn_login.setText("登陆");
                    bookLoading.setVisibility(View.GONE);
                    bookLoading.stop();
                }
            });
            getData();
        }
    };
    Timer timer = new Timer();
    timer.schedule(task, 3000);

}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        init_view();
        get_username_password();
        bookLoading= (RotateLoading) findViewById(R.id.bookloading);
    }

    /**
     * 点击注册按钮，进入注册页面
     */
    private void init_view() {
        TextView btn=findViewById(R.id.btn_register);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }

        });
    }
    /**
     * 从Bmob查询数据
     */
    private  void getData(){
        BmobQuery<User> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("username",et_login.getText().toString());
        categoryBmobQuery.addWhereEqualTo("password",et_pass.getText().toString());
        categoryBmobQuery.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> object, BmobException e) {
                if (e == null) {
                    String gname=object.get(0).getUsername().toString();
                    String gpassword=object.get(0).getPassword().toString();
                    String String_name=et_login.getText().toString();
                    String String_password=et_pass.getText().toString();
                    if(gname.equals(String_name)&&gpassword.equals(String_password)){
                     //   DialogUtil.dialogCreate(LoginActivity.this,"登录成功","登录成功", PromptDialog.DIALOG_TYPE_HELP,"ok");
                        editor = getSharedPreferences("user",MODE_PRIVATE).edit();
                        if(cb_rm.isChecked()){
                            editor.putString("username",gname);
                            editor.putString("password",gpassword);
                            editor.commit();
                        }
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    }

                } else {
                    DialogUtil.dialogCreate(LoginActivity.this,"登录失败","用户名或密码错误", PromptDialog.DIALOG_TYPE_WRONG,"确定");
                }
            }
        });
    }

    /**
     * 从shareperence中获取记住密码之后的值
     */
    private void get_username_password() {
        sharedPreferences =getSharedPreferences("user",MODE_PRIVATE);
        String set_username =sharedPreferences.getString("username","");
        et_login.setText(set_username);
        String set_password =sharedPreferences.getString("password","");
        et_pass.setText(set_password);
    }
}
